﻿#include "DialogLogin.h"
#include "ui_DialogLogin.h"
#include "qmessagebox.h"
#include <qevent.h>

#include "MainWindow.h"
#include "UserRegisterCodec.h"
#include "TcpIOer.h"
#include "UserInfoCodec.h"

#include "MacroDefinition.h"

#include <qdebug.h>

DialogLogin::DialogLogin(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::DialogLogin)
{
  ui->setupUi(this);
  setFixedSize(size());
  connect(ui->pushButton_login,&QPushButton::clicked,this,&DialogLogin::slot_pushButton_login);
  connect(ui->pushButton_exit,&QPushButton::clicked,this,&DialogLogin::slot_pushButton_exit);
  ui->lineEdit_userName->setText("admin");
  ui->lineEdit_password->setText("admin");
}

DialogLogin::~DialogLogin()
{
  delete ui;
}

void DialogLogin::slot_pushButton_login()
{
  QString userName=ui->lineEdit_userName->text();
  QString password=ui->lineEdit_password->text();
  if(userName.isEmpty())
    {
      QMessageBox::warning(this,QStringLiteral("警告"),QStringLiteral("用户名不能为空"));
      return;
    }
  if(password.isEmpty())
    {
      QMessageBox::warning(this,QStringLiteral("警告"),QStringLiteral("密码不能为空"));
      return;
    }
  if(tcpIOer->state()!=TcpIOer::ConnectedState)
    {
      tcpIOer->connectToHost(SERVERHOST,SERVERPORT);
    }
  if(tcpIOer->waitForConnected(2000))
    {
      QTimer::singleShot(100,[=](){userRegisterCodec->login(userName,password);});
    }
  else
    {
      QMessageBox::critical(this,QStringLiteral("错误"),QStringLiteral("无法连接到服务器, 请检查网络"));
    }
}

void DialogLogin::slot_pushButton_exit()
{
  close();
}

void DialogLogin::slot_handleResult(uchar result)
{
  if(!isVisible())
    {
      return;
    }
  switch (result)
    {
    case UserRegisterCodec::Register:
      this->hide();
      mainWindow->show();
      break;
    case UserRegisterCodec::Unregister:
      QMessageBox::information(this,QStringLiteral("消息"),QStringLiteral("登陆失败，账号密码错误"));
      break;
    case UserRegisterCodec::Registed:
      QMessageBox::information(this,QStringLiteral("消息"),QStringLiteral("登陆失败，该账号已经登录"));
      break;
    default:
      break;
    }
}

void DialogLogin::closeEvent(QCloseEvent * closeEvent)
{
  if(QMessageBox::question(this,QStringLiteral("退出"),QStringLiteral("确定要退出吗？"))==QMessageBox::Yes)
    {
      closeEvent->accept();
    }
  else
    {
      closeEvent->ignore();
    }
}

MainWindow* DialogLogin::getMainWindow()const
{
  return mainWindow;
}


DialogLogin* DialogLogin::setMainWindow(MainWindow* const mainWindow)
{
  this->mainWindow=mainWindow;
  printInjection(this,mainWindow);
  return this;
}

UserRegisterCodec* DialogLogin::getUserRegisterCodec()const
{
  return userRegisterCodec;
}

DialogLogin* DialogLogin::setUserRegisterCodec(UserRegisterCodec* const userRegisterCodec)
{
  if(Q_NULLPTR!=this->userRegisterCodec)
    {
      disconnect(this->userRegisterCodec,&UserRegisterCodec::signal_registerState,this,&DialogLogin::slot_handleResult);
    }
  this->userRegisterCodec=userRegisterCodec;
  if(Q_NULLPTR!=this->userRegisterCodec)
    {
      connect(this->userRegisterCodec,&UserRegisterCodec::signal_registerState,this,&DialogLogin::slot_handleResult);
    }
  printInjection(this,userRegisterCodec);
  return this;
}

TcpIOer* DialogLogin::getTcpIOer()const
{
  return tcpIOer;
}

DialogLogin* DialogLogin::setTcpIOer(TcpIOer* const tcpIOer)
{
  this->tcpIOer=tcpIOer;
  printInjection(this,tcpIOer);
  return this;
}

UserInfoCodec* DialogLogin::getUserInfoCodec()const
{
  return userInfoCodec;
}

DialogLogin* DialogLogin::setUserInfoCodec(UserInfoCodec* const userInfoCodec)
{
  this->userInfoCodec=userInfoCodec;
  printInjection(this,userInfoCodec);
  return this;
}

void DialogLogin::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setMainWindow(getObjFromMap<MainWindow>(objMap));
  setUserRegisterCodec(getObjFromMap<UserRegisterCodec>(objMap));
  setTcpIOer(getObjFromMap<TcpIOer>(objMap));
  setUserInfoCodec(getObjFromMap<UserInfoCodec>(objMap));
}

